package org.example.dingtalkintegrationservice.web;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
//import org.example.dingtalkintegrationservice.exception.BodyMissingException;
import org.example.dingtalkintegrationservice.dingtalk.dingback.HandleDingBack;
import org.example.dingtalkintegrationservice.dingtalk.process.CreateProcess;
import org.example.dingtalkintegrationservice.dingtalk.process.CreateProcessPrepare;
import org.example.dingtalkintegrationservice.dingtalk.processInstance.CreateProcessInstance;
import org.example.dingtalkintegrationservice.dingtalk.processInstance.CreateProcessInstancePrepare;
import org.example.dingtalkintegrationservice.dingtalk.processTask.CreateProcessTask;
import org.example.dingtalkintegrationservice.dingtalk.processTask.UpdateProcessTask;
import org.example.dingtalkintegrationservice.dingtalk.token.GetDingToken;
import org.example.dingtalkintegrationservice.dingtalk.user.GetAllUserBaseInfo;
import org.example.dingtalkintegrationservice.pojo.DingBack;
import org.example.dingtalkintegrationservice.pojo.IdRecord;
import org.example.dingtalkintegrationservice.repository.IdRecordRepository;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author pc
 */
@RestController
@CrossOrigin
@Slf4j
public class DingTalkController {
    @Resource
    GetDingToken getDingToken;
    @Resource
    IdRecordRepository idRecordRepository;
    @Resource
    CreateProcess createProcess;
    @Resource
    CreateProcessInstance createProcessInstance;
    @Resource
    CreateProcessTask createProcessTask;
    @Resource
    UpdateProcessTask updateProcessTask;
    @Resource
    GetAllUserBaseInfo getAllUserBaseInfo;
    @Resource
    CreateProcessPrepare createProcessPrepare;
    @Resource
    CreateProcessInstancePrepare createProcessInstancePrepare;
    @Resource
    HandleDingBack handleDingBack;
    @GetMapping("/getToken")
    public String getToken(){
        return getDingToken.getTokenFromRedis();
    }
    @PostMapping("/createProcess")
    public String createProcess(@RequestParam("name")String name,@RequestParam("description")String description,@RequestParam("id")String classCode,@RequestBody String input) throws Exception {
        String token = getDingToken.getTokenFromRedis();
        input = createProcessPrepare.convert(input);
        return createProcess.exec(classCode,token,name,description,input);
//        Optional<IdRecord> idRecord = idRecordRepository.findLatestById1(classCode);
//        if (idRecord.isPresent()) {
//            String processId = idRecord.get().getId2();
//            return createProcess.update(classCode,token, name, description, processId, input);
//        } else {
//            return createProcess.create(classCode,token, name, description, input);
//        }
    }
    @PostMapping("/createProcessInstance")
    public String createProcessInstance(@RequestParam("classcode")String classCode,@RequestParam("originator_user_id")String originatorUserId,@RequestParam("title")String title,@RequestParam("flow_id")String flowId,@RequestBody String input) throws Exception {
//        if (Objects.equals(input, "")){
//            throw new BodyMissingException();
//        }
        String token = getDingToken.getTokenFromRedis();
        input= createProcessInstancePrepare.convert(input);
        return createProcessInstance.exec(input,token,originatorUserId,title,flowId,classCode);
        }
        @PostMapping("/createProcessTask")
    public String createProcessTask(@RequestParam("user_id")String userId,@RequestParam("activity_id")String activityId,@RequestParam("flow_id")String flowId) throws Exception {
            String token = getDingToken.getTokenFromRedis();
        return createProcessTask.exec(token,userId,flowId,activityId);
    }
    @PostMapping("/updateTask")
    public String updateTask(@RequestParam("result")String result,@RequestParam("flow_id")String flowId,@RequestParam("plm_task_id")String plmTaskId,@RequestParam(value = "user_id",required = false)String userId) throws Exception {
        String token = getDingToken.getTokenFromRedis();
        return updateProcessTask.exec(token,flowId,plmTaskId,userId,result);
    }
    @PostMapping("/getAllUserInfo")
    public String getAllUserInfo(){
        String token = getDingToken.getTokenFromRedis();
        return getAllUserBaseInfo.getAllUserBaseInfoFromRedis(token);
    }
//    @PostMapping("/handleDingCallBack")
//    public DingBack handleDingCallBack(@RequestBody String input){
//        return handleDingBack.exec(input);
//    }
    @PostMapping("/test")
    public DingBack handleDingCallBackTest(@RequestBody String input){
        return handleDingBack.exec(input);
    }
}